<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 站点配置相关Controller v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\controller\site;


use app\core\model\SiteConfig;

class ConfigController extends SiteBaseController {

    /**
     * ConfigController constructor.
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * 站点设置页面
     *
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index() {
        if (is_submit()) {
            $cfs = input();
            foreach ($cfs as $name => $value) {
                SiteConfig::set($name, $value, false);
            }
            SiteConfig::reload();
            $this->success("保存成功");
        } else {
            $cfs = SiteConfig::$cfs;
            $configValues = SiteConfig::get();

            $configGroups = [];
            foreach ($cfs as $name => $cf) {
                if (!isset($configGroups[$cf['group']])) {
                    $configGroups[$cf['group']] = [];
                }
                $cf = array_merge($cf, [
                    'name' => $name,
                    'value' => fetch($configValues, $name, $cf['defaultVal'])
                ]);
                if (!isset($cf['type'])) {
                    $cf['type'] = 'input';
                }
                if (!isset($cf['tip'])) {
                    $cf['tip'] = '';
                }
                $configGroups[$cf['group']][] = $cf;
            }
            $this->assign("configGroups", $configGroups);
            return view();
        }
    }
}